#include <Lucnx/printf.h>
#include <Lucnx/stdlib.h>
#include <Lucnx/syscall.h>
#include <Lucnx/memory.h>
#include <Lucnx/task.h>
#include <Lucnx/io.h>
#include <Lucnx/interrupt.h>
#include <Lucnx/string.h>
#include <Lucnx/ide.h>
#include <Lucnx/debug.h>
#include <Lucnx/printk.h>
#include <Lucnx/console.h>

void ush_main();

extern ide_ctrl_t ctrls[2];
task_t *idle_pcb;
task_t *task_b_pcb;

void idle_task()
{
    while (true)
    {
        asm volatile
            ("sti\n"
            "hlt\n");
    }
}

void user_init_thread()
{
    exit();
}

void init_thread()
{
    ush_main();
}

void user_test_thread()
{
    printf("start fork\n");
    pid_t pid = fork();
    if (pid)
    {
        brk(USER_MEMORY + 0x1000);
        printf("I'm father, my pid is %d, my son pid is %d\n", getpid(), pid);
        while (1);
    }
    else
    {
        brk(USER_MEMORY + 0x2000);
        printf("I'm son, my pid is %d my father pid is %d\n", getpid(), getppid());
        while (true);
    }
}

void test_thread()
{
    task_to_user_mode(user_test_thread);
}

void thread_init()
{
    task_create(init_thread, "init", 10);
    task_create(test_thread, "test", 3);

    idle_pcb = task_create(idle_task, "idle", 1);
}